import { Component, OnInit, Input, Output, EventEmitter, Injectable } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { _HttpClient } from '@delon/theme';
import { environment } from '../../../../environments/environment';

@Component({
    selector: 'app-table',
    templateUrl: './table.component.html',
    styles: [`
        .wd{width:100%;}
        .wd70{width:70px;}
        .wd150{width:150px;}
        .round{display:block;width:20px;height:20px;line-height:20px;margin:6px auto 0 auto;border-radius:50%;}        
        .center{text-align:center;}
        .ant-input{padding:4px 8px;border:1px solid #f5fafd;background:#f5fafd;}
        :host ::ng-deep .ant-select-selection-selected-value{width:100%;}
        :host ::ng-deep .ant-calendar-picker-input{padding-left:17px;}
        :host ::ng-deep .ant-time-picker-input{padding-left:17px;}
        :host ::ng-deep .ant-select-allow-clear .ant-select-selection--single .ant-select-selection-selected-value{padding-right:0;}
    `]
})

export class TableComponent implements OnInit {
    @Input() paramType: any;
    @Input() pageType: any;

    bodyData: any = [];
    allChecked = false;
    indeterminate = false;
    displayData = [];
    selectedValue = '20';
    pageTotal = '';
    rowTotalTable = '0';
    pageValue = '1';
    lockHeadScroll = { y: window.innerHeight - 350 + 'px' };

    apiUrl = '';
    apiParam = '';
    tipsText = '';
    loading = true;
    headData: any = [];
    wcztSelect = [{ "value": "1", "label": "完成" }, { "value": "0", "label": "未完成" }];
    xbSelect = [];
    czSelect = [];
    timeStatus = true;
    treeNodeStyle = { 'max-width': '300px', 'max-height': '400px', 'overflow-y': 'auto', 'overflow-x': 'hidden' };

    constructor(
        public msg: NzMessageService,
        private http: _HttpClient
    ) { }

    ngOnInit() {
        switch (this.pageType) {
            case 'safetyAnalyse':
                this.headData = [
                    { "name": "序号", "width": "50px" },
                    { "name": "日期", "width": "135px" },
                    { "name": "线别", "width": "15%" },
                    { "name": "车站", "width": "20%" },
                    { "name": "分析时间", "width": "200px" },
                    { "name": "分析人", "width": "10%" },
                    { "name": "完成状态", "width": "10%" },
                    { "name": "备注", "width": "" }
                ];
                break;
            case 'rotaryAnalyse':
                this.headData = [
                    { "name": "序号", "width": "50px" },
                    { "name": "日期", "width": "135px" },
                    { "name": "线别", "width": "15%" },
                    { "name": "车站", "width": "20%" },
                    { "name": "分析时间", "width": "200px" },
                    { "name": "分析人", "width": "10%" },
                    { "name": "完成状态", "width": "10%" },
                    { "name": "备注", "width": "" }
                ];
                break;
            case 'dmsAnalyse':
            case 'lkjAnalyse':
                this.headData = [
                    { "name": "序号", "width": "50px" },
                    { "name": "日期", "width": "135px" },
                    { "name": "车次", "width": "15%" },
                    { "name": "车号", "width": "15%" },
                    { "name": "分析时间", "width": "200px" },
                    { "name": "分析人", "width": "10%" },
                    { "name": "完成状态", "width": "10%" },
                    { "name": "备注", "width": "" }
                ];
                break;
            case 'eoasAnalyse':
                this.headData = [
                    { "name": "序号", "width": "50px" },
                    { "name": "日期", "width": "135px" },
                    { "name": "线别", "width": "15%" },
                    { "name": "车次", "width": "15%" },
                    { "name": "分析时间", "width": "200px" },
                    { "name": "分析人", "width": "10%" },
                    { "name": "完成状态", "width": "10%" },
                    { "name": "备注", "width": "" }
                ];
                break;
            case 'videoAnalyse':
                this.headData = [
                    { "name": "序号", "width": "50px" },
                    { "name": "日期", "width": "135px" },
                    { "name": "线别", "width": "15%" },
                    { "name": "车站", "width": "20%" },
                    { "name": "分析时间", "width": "200px" },
                    { "name": "分析人", "width": "10%" },
                    { "name": "完成状态", "width": "10%" },
                    { "name": "备注", "width": "" }
                ];
                break;
        }
    }
    updateData(data, name) {
        if (this.timeStatus) {
            this.timeStatus = false;
            setTimeout(() => {
                if (data[name]) {
                    const url = environment.project + '/syntheticAnalysis/update';
                    const param: any = { id: data.id, 'type': this.paramType };
                    this.tipsText = '序号' + data.number + ' ';
                    switch (name) {
                        case 'analysisDate':
                            param.analysisDate = this.timeToDate(data[name], '');
                            this.tipsText += '日期：' + param.analysisDate;
                            break;
                        case 'line':
                            param.line = data[name];
                            this.tipsText += '线别：' + param.line;
                            break;
                        case 'trainNo':
                            param.trainNo = data[name];
                            this.tipsText += '车次：' + param.trainNo;
                            break;
                        case 'trainNum':
                            param.trainNum = data[name];
                            this.tipsText += '车号：' + param.trainNum;
                            break;
                        case 'startStation':
                            param.startStation = data[name];
                            this.tipsText += '起始车站：' + param.startStation;
                            break;
                        case 'endStation':
                            param.endStation = data[name];
                            this.tipsText += '末尾车站：' + param.endStation;
                            break;
                        case 'startTime':
                            param.startTime = this.timeToDate(data[name], 'hhmm');
                            this.tipsText += '开始时间：' + param.startTime;
                            break;
                        case 'endTime':
                            param.endTime = this.timeToDate(data[name], 'hhmm');
                            this.tipsText += '结束时间：' + param.endTime;
                            break;
                        case 'analysisUser':
                            param.analysisUser = data[name];
                            this.tipsText += '分析人：' + param.analysisUser;
                            break;
                        case 'finished':
                            param.finished = data[name];
                            this.tipsText += '完成情况：' + param.finished;
                            break;
                        case 'remark':
                            param.remark = data[name];
                            this.tipsText += '备注：' + param.remark;
                            break;
                    }
                    this.tipsText += ' 提交错误。请刷新后再试';
                    this.http.post(url, param).subscribe((res: any) => {
                        if (res.code) {
                            this.msg.error(this.tipsText, { nzDuration: 3000 });
                        }
                    });
                }
                this.timeStatus = true;
            }, 200);
        }
    }
    loadList(url, param) {
        this.apiUrl = url;
        this.apiParam = param;
        const newParam = '?page=' + this.pageValue + '&limit=' + this.selectedValue + param;
        // 请求后获取的数据
        this.http.get(url + newParam).subscribe((res: any) => {
            if (!res.code) {
                this.bodyData = res.data;
                let index = 0;
                for (const item of this.bodyData) {
                    item.number = (Number(this.pageValue) - 1) * Number(this.selectedValue) + index + 1;
                    if (item.startTime) {
                        item.startTime = this.timeToDate(new Date, '') + ' ' + item.startTime + ':00';
                    }
                    if (item.endTime) {
                        item.endTime = this.timeToDate(new Date, '') + ' ' + item.endTime + ':00';
                    }
                    index += 1;
                }
                this.pageTotal = res.count;
                this.rowTotalTable = res.count;
            }
            this.loading = false;
        });
    }
    addList(callback) {
        const url = environment.project + '/syntheticAnalysis/add';
        let param = {};
        switch (this.pageType) {
            case 'safetyAnalyse':
                param = {
                    'analysisDate': '',
                    'line': '',
                    'startTime': null,
                    'endTime': null,
                    'analysisUser': '',
                    'finished': '0',
                    'remark': '',
                    'type': this.paramType
                };
                break;
            case 'rotaryAnalyse':
                param = {
                    'analysisDate': '',
                    'line': '',
                    'startStation': '',
                    'endStation': '',
                    'startTime': null,
                    'endTime': null,
                    'analysisUser': '',
                    'finished': '0',
                    'remark': '',
                    'type': this.paramType
                };
                break;
            case 'dmsAnalyse':
                param = {
                    'analysisDate': '',
                    'trainNo': '',
                    'trainNum': '',
                    'startTime': null,
                    'endTime': null,
                    'analysisUser': '',
                    'finished': '0',
                    'remark': '',
                    'type': this.paramType
                };
                break;
            case 'lkjAnalyse':
                param = {
                    'analysisDate': '',
                    'trainNo': '',
                    'trainNum': '',
                    'startTime': null,
                    'endTime': null,
                    'analysisUser': '',
                    'finished': '0',
                    'remark': '',
                    'type': this.paramType
                };
                break;
            case 'eoasAnalyse':
                param = {
                    'analysisDate': '',
                    'line': '',
                    'trainNo': '',
                    'startTime': null,
                    'endTime': null,
                    'analysisUser': '',
                    'finished': '0',
                    'remark': '',
                    'type': this.paramType
                };
                break;
            case 'videoAnalyse':
                param = {
                    'analysisDate': '',
                    'line': '',
                    'startStation': '',
                    'startTime': null,
                    'endTime': null,
                    'analysisUser': '',
                    'finished': '0',
                    'remark': '',
                    'type': this.paramType
                };
                break;
        }
        this.http.post(url, param).subscribe((res: any) => {
            setTimeout(() => {
                if (!res.code) {
                    this.loadList(this.apiUrl, this.apiParam);
                } else {
                    this.msg.error('新增错误，请刷新后再试', { nzDuration: 3000 });
                }
                callback(res);
            }, 200);
        });
    }
    timeToDate(time, type) {
        const d = new Date(time);
        const YY = d.getFullYear();
        const MM = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1);
        const DD = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
        const hh = d.getHours();
        const mm = d.getMinutes();
        const ss = d.getSeconds();
        switch (type) {
            case 'full':
                return YY + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss;
            case 'hhmm':
                return hh + ':' + mm;
            default:
                return YY + '-' + MM + '-' + DD;
        }
    }
    getAllData() {
        return this.displayData;
    }
    rowChangeTable(ev) {
        this.pageValue = ev;
        this.loadList(this.apiUrl, this.apiParam);
    }
    selectedChange() {
        this.pageValue = '1';
        this.loadList(this.apiUrl, this.apiParam);
    }
    currentPageDataChange($event: Array<{ name: string; age: number; address: string; checked: boolean; disabled: boolean; }>): void {
        this.displayData = $event;
        this.refreshStatus();
    }
    refreshStatus(): void {
        const allChecked = this.displayData.filter(value => !value.disabled).every(value => value.checked === true);
        const allUnChecked = this.displayData.filter(value => !value.disabled).every(value => !value.checked);
        this.allChecked = allChecked;
        this.indeterminate = (!allChecked) && (!allUnChecked);
    }
    checkAll(value: boolean): void {
        this.displayData.forEach(data => {
            if (!data.disabled) {
                data.checked = value;
            }
        });
        this.refreshStatus();
    }
}
